<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book django项目 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>类视图 | django项目</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C04-ClassViewAndMiddleware/Middleware.html" />
    
    
    <link rel="prev" href="../C04-ClassViewAndMiddleware/index.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="5.1"
        data-chapter-title="类视图"
        data-filepath="C04-ClassViewAndMiddleware/ClassView.md"
        data-basepath=".."
        data-revision="Wed Nov 14 2018 07:27:32 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python Web 框架要点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutDjango.html">
            
                
                    <a href="../AboutDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/StartProject.html">
            
                
                    <a href="../C01-CreateProject/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/StartApp.html">
            
                
                    <a href="../C01-CreateProject/StartApp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        创建子应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/CreateView.html">
            
                
                    <a href="../C01-CreateProject/CreateView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        创建视图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-SettingsStaticAndURL/index.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        配置、静态文件与路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-SettingsStaticAndURL/Settings.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/Settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-SettingsStaticAndURL/StaticFiles.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/StaticFiles.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C02-SettingsStaticAndURL/URL.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/URL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        路由说明
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/Request.html">
            
                
                    <a href="../C03-RequestAndResponse/Request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        请求Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/Response.html">
            
                
                    <a href="../C03-RequestAndResponse/Response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        响应Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/Cookie.html">
            
                
                    <a href="../C03-RequestAndResponse/Cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C03-RequestAndResponse/Session.html">
            
                
                    <a href="../C03-RequestAndResponse/Session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-ClassViewAndMiddleware/index.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        类视图与中间件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="5.1" data-path="C04-ClassViewAndMiddleware/ClassView.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/ClassView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-ClassViewAndMiddleware/Middleware.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Template/index.html">
            
                
                    <a href="../C05-Template/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Template/template.html">
            
                
                    <a href="../C05-Template/template.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        模板使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C06-Database/index.html">
            
                
                    <a href="../C06-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C06-Database/Config.html">
            
                
                    <a href="../C06-Database/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="C06-Database/Model.html">
            
                
                    <a href="../C06-Database/Model.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="C06-Database/Tools.html">
            
                
                    <a href="../C06-Database/Tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        演示工具使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="C06-Database/CURD.html">
            
                
                    <a href="../C06-Database/CURD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        数据库操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="C06-Database/QuerySet.html">
            
                
                    <a href="../C06-Database/QuerySet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        查询集QuerySet
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C07-Admin/index.html">
            
                
                    <a href="../C07-Admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        Admin站点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C07-Admin/Config.html">
            
                
                    <a href="../C07-Admin/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        使用Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="C07-Admin/ModifyListDisplay.html">
            
                
                    <a href="../C07-Admin/ModifyListDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        调整列表页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="C07-Admin/ModifyEditDisplay.html">
            
                
                    <a href="../C07-Admin/ModifyEditDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        调整编辑页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="C07-Admin/ModifySite.html">
            
                
                    <a href="../C07-Admin/ModifySite.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        调整站点信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="C07-Admin/UploadImage.html">
            
                
                    <a href="../C07-Admin/UploadImage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >django项目</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x7C7B;&#x89C6;&#x56FE;">&#x7C7B;&#x89C6;&#x56FE;</h1>
<h2 id="1-&#x7C7B;&#x89C6;&#x56FE;&#x5F15;&#x5165;">1 &#x7C7B;&#x89C6;&#x56FE;&#x5F15;&#x5165;</h2>
<p>&#x4EE5;&#x51FD;&#x6570;&#x7684;&#x65B9;&#x5F0F;&#x5B9A;&#x4E49;&#x7684;&#x89C6;&#x56FE;&#x79F0;&#x4E3A;<strong>&#x51FD;&#x6570;&#x89C6;&#x56FE;</strong>&#xFF0C;&#x51FD;&#x6570;&#x89C6;&#x56FE;&#x4FBF;&#x4E8E;&#x7406;&#x89E3;&#x3002;&#x4F46;&#x662F;&#x9047;&#x5230;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#x5BF9;&#x5E94;&#x7684;&#x8DEF;&#x5F84;&#x63D0;&#x4F9B;&#x4E86;&#x591A;&#x79CD;&#x4E0D;&#x540C;HTTP&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x7684;&#x652F;&#x6301;&#x65F6;&#xFF0C;&#x4FBF;&#x9700;&#x8981;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x7F16;&#x5199;&#x4E0D;&#x540C;&#x7684;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#xFF0C;&#x4EE3;&#x7801;&#x53EF;&#x8BFB;&#x6027;&#x4E0E;&#x590D;&#x7528;&#x6027;&#x90FD;&#x4E0D;&#x4F73;&#x3002;</p>
<pre><code class="lang-python"> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">register</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x5904;&#x7406;&#x6CE8;&#x518C;&quot;&quot;&quot;</span>

    <span class="hljs-comment"># &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#xFF0C;&#x5224;&#x65AD;&#x662F;GET/POST&#x8BF7;&#x6C42;</span>
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">&apos;GET&apos;</span>:
        <span class="hljs-comment"># &#x5904;&#x7406;GET&#x8BF7;&#x6C42;&#xFF0C;&#x8FD4;&#x56DE;&#x6CE8;&#x518C;&#x9875;&#x9762;</span>
        <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;register.html&apos;</span>)
    <span class="hljs-keyword">else</span>:
        <span class="hljs-comment"># &#x5904;&#x7406;POST&#x8BF7;&#x6C42;&#xFF0C;&#x5B9E;&#x73B0;&#x6CE8;&#x518C;&#x903B;&#x8F91;</span>
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;&#x8FD9;&#x91CC;&#x5B9E;&#x73B0;&#x6CE8;&#x518C;&#x903B;&#x8F91;&apos;</span>)
</code></pre>
<p>&#x5728;Django&#x4E2D;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7C7B;&#x6765;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#xFF0C;&#x79F0;&#x4E3A;<strong>&#x7C7B;&#x89C6;&#x56FE;</strong>&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x7C7B;&#x89C6;&#x56FE;&#x53EF;&#x4EE5;&#x5C06;&#x89C6;&#x56FE;&#x5BF9;&#x5E94;&#x7684;&#x4E0D;&#x540C;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x4EE5;&#x7C7B;&#x4E2D;&#x7684;&#x4E0D;&#x540C;&#x65B9;&#x6CD5;&#x6765;&#x533A;&#x522B;&#x5B9A;&#x4E49;&#x3002;&#x5982;&#x4E0B;&#x6240;&#x793A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.views.generic <span class="hljs-keyword">import</span> View

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RegisterView</span><span class="hljs-params">(View)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x7C7B;&#x89C6;&#x56FE;&#xFF1A;&#x5904;&#x7406;&#x6CE8;&#x518C;&quot;&quot;&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x5904;&#x7406;GET&#x8BF7;&#x6C42;&#xFF0C;&#x8FD4;&#x56DE;&#x6CE8;&#x518C;&#x9875;&#x9762;&quot;&quot;&quot;</span>
        <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;register.html&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x5904;&#x7406;POST&#x8BF7;&#x6C42;&#xFF0C;&#x5B9E;&#x73B0;&#x6CE8;&#x518C;&#x903B;&#x8F91;&quot;&quot;&quot;</span>
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;&#x8FD9;&#x91CC;&#x5B9E;&#x73B0;&#x6CE8;&#x518C;&#x903B;&#x8F91;&apos;</span>)
</code></pre>
<p>&#x7C7B;&#x89C6;&#x56FE;&#x7684;&#x597D;&#x5904;&#xFF1A;</p>
<ul>
<li><strong>&#x4EE3;&#x7801;&#x53EF;&#x8BFB;&#x6027;&#x597D;</strong></li>
<li><strong>&#x7C7B;&#x89C6;&#x56FE;&#x76F8;&#x5BF9;&#x4E8E;&#x51FD;&#x6570;&#x89C6;&#x56FE;&#x6709;&#x66F4;&#x9AD8;&#x7684;&#x590D;&#x7528;&#x6027;</strong>&#xFF0C; &#x5982;&#x679C;&#x5176;&#x4ED6;&#x5730;&#x65B9;&#x9700;&#x8981;&#x7528;&#x5230;&#x67D0;&#x4E2A;&#x7C7B;&#x89C6;&#x56FE;&#x7684;&#x67D0;&#x4E2A;&#x7279;&#x5B9A;&#x903B;&#x8F91;&#xFF0C;&#x76F4;&#x63A5;&#x7EE7;&#x627F;&#x8BE5;&#x7C7B;&#x89C6;&#x56FE;&#x5373;&#x53EF;</li>
</ul>
<h2 id="2-&#x7C7B;&#x89C6;&#x56FE;&#x4F7F;&#x7528;">2 &#x7C7B;&#x89C6;&#x56FE;&#x4F7F;&#x7528;</h2>
<p>&#x5B9A;&#x4E49;&#x7C7B;&#x89C6;&#x56FE;&#x9700;&#x8981;&#x7EE7;&#x627F;&#x81EA;Django&#x63D0;&#x4F9B;&#x7684;&#x7236;&#x7C7B;<strong>View</strong>&#xFF0C;&#x53EF;&#x4F7F;&#x7528;<code>from django.views.generic import View</code>&#x6216;&#x8005;<code>from django.views.generic.base import View</code> &#x5BFC;&#x5165;&#xFF0C;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x5982;&#x4E0A;&#x6240;&#x793A;&#x3002;</p>
<p><strong>&#x914D;&#x7F6E;&#x8DEF;&#x7531;&#x65F6;&#xFF0C;&#x4F7F;&#x7528;&#x7C7B;&#x89C6;&#x56FE;&#x7684;<code>as_view()</code>&#x65B9;&#x6CD5;&#x6765;&#x6DFB;&#x52A0;</strong>&#x3002;</p>
<pre><code class="lang-python">urlpatterns = [
    <span class="hljs-comment"># &#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF1A;&#x6CE8;&#x518C;</span>
    <span class="hljs-comment"># url(r&apos;^register/$&apos;, views.register, name=&apos;register&apos;),</span>
    <span class="hljs-comment"># &#x7C7B;&#x89C6;&#x56FE;&#xFF1A;&#x6CE8;&#x518C;</span>
    url(<span class="hljs-string">r&apos;^register/$&apos;</span>, views.RegisterView.as_view(), name=<span class="hljs-string">&apos;register&apos;</span>),
]
</code></pre>
<h2 id="3-&#x7C7B;&#x89C6;&#x56FE;&#x539F;&#x7406;">3 &#x7C7B;&#x89C6;&#x56FE;&#x539F;&#x7406;</h2>
<pre><code class="lang-python"><span class="hljs-decorator">    @classonlymethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">as_view</span><span class="hljs-params">(cls, **initkwargs)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        Main entry point for a request-response process.
        &quot;&quot;&quot;</span>
        ...&#x7701;&#x7565;&#x4EE3;&#x7801;...

        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view</span><span class="hljs-params">(request, *args, **kwargs)</span>:</span>
            self = cls(**initkwargs)
            <span class="hljs-keyword">if</span> hasattr(self, <span class="hljs-string">&apos;get&apos;</span>) <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> hasattr(self, <span class="hljs-string">&apos;head&apos;</span>):
                self.head = self.get
            self.request = request
            self.args = args
            self.kwargs = kwargs
            <span class="hljs-comment"># &#x8C03;&#x7528;dispatch&#x65B9;&#x6CD5;&#xFF0C;&#x6309;&#x7167;&#x4E0D;&#x540C;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x8C03;&#x7528;&#x4E0D;&#x540C;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;</span>
            <span class="hljs-keyword">return</span> self.dispatch(request, *args, **kwargs)

        ...&#x7701;&#x7565;&#x4EE3;&#x7801;...

        <span class="hljs-comment"># &#x8FD4;&#x56DE;&#x771F;&#x6B63;&#x7684;&#x51FD;&#x6570;&#x89C6;&#x56FE;</span>
        <span class="hljs-keyword">return</span> view


    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">dispatch</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        <span class="hljs-comment"># Try to dispatch to the right method; if a method doesn&apos;t exist,</span>
        <span class="hljs-comment"># defer to the error handler. Also defer to the error handler if the</span>
        <span class="hljs-comment"># request method isn&apos;t on the approved list.</span>
        <span class="hljs-keyword">if</span> request.method.lower() <span class="hljs-keyword">in</span> self.http_method_names:
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
        <span class="hljs-keyword">else</span>:
            handler = self.http_method_not_allowed
        <span class="hljs-keyword">return</span> handler(request, *args, **kwargs)
</code></pre>
<h2 id="4-&#x7C7B;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;">4 &#x7C7B;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;</h2>
<p>&#x4E3A;&#x7C7B;&#x89C6;&#x56FE;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x7406;&#x89E3;&#x65B9;&#x4FBF;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x6765;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;<strong>&#x4E3A;&#x51FD;&#x6570;&#x89C6;&#x56FE;&#x51C6;&#x5907;&#x7684;&#x88C5;&#x9970;&#x5668;</strong>&#xFF08;&#x5728;&#x8BBE;&#x8BA1;&#x88C5;&#x9970;&#x5668;&#x65F6;&#x57FA;&#x672C;&#x90FD;&#x4EE5;&#x51FD;&#x6570;&#x89C6;&#x56FE;&#x4F5C;&#x4E3A;&#x8003;&#x8651;&#x7684;&#x88AB;&#x88C5;&#x9970;&#x5BF9;&#x8C61;&#xFF09;&#xFF0C;&#x53CA;&#x4E00;&#x4E2A;&#x8981;&#x88AB;&#x88C5;&#x9970;&#x7684;&#x7C7B;&#x89C6;&#x56FE;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_decorator</span><span class="hljs-params">(func)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span><span class="hljs-params">(request, *args, **kwargs)</span>:</span>
        print(<span class="hljs-string">&apos;&#x81EA;&#x5B9A;&#x4E49;&#x88C5;&#x9970;&#x5668;&#x88AB;&#x8C03;&#x7528;&#x4E86;&apos;</span>)
        print(<span class="hljs-string">&apos;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;%s&apos;</span> % request.path)
        <span class="hljs-keyword">return</span> func(request, *args, **kwargs)
    <span class="hljs-keyword">return</span> wrapper

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoView</span><span class="hljs-params">(View)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        print(<span class="hljs-string">&apos;get&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        print(<span class="hljs-string">&apos;post&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)
</code></pre>
<h3 id="41-&#x5728;url&#x914D;&#x7F6E;&#x4E2D;&#x88C5;&#x9970;">4.1 &#x5728;URL&#x914D;&#x7F6E;&#x4E2D;&#x88C5;&#x9970;</h3>
<pre><code class="lang-python">urlpatterns = [
    url(<span class="hljs-string">r&apos;^demo/$&apos;</span>, my_decorate(DemoView.as_view()))
]
</code></pre>
<p>&#x6B64;&#x79CD;&#x65B9;&#x5F0F;&#x6700;&#x7B80;&#x5355;&#xFF0C;&#x4F46;&#x56E0;&#x88C5;&#x9970;&#x884C;&#x4E3A;&#x88AB;&#x653E;&#x7F6E;&#x5230;&#x4E86;url&#x914D;&#x7F6E;&#x4E2D;&#xFF0C;&#x5355;&#x770B;&#x89C6;&#x56FE;&#x7684;&#x65F6;&#x5019;&#x65E0;&#x6CD5;&#x77E5;&#x9053;&#x6B64;&#x89C6;&#x56FE;&#x8FD8;&#x88AB;&#x6DFB;&#x52A0;&#x4E86;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x4E0D;&#x5229;&#x4E8E;&#x4EE3;&#x7801;&#x7684;&#x5B8C;&#x6574;&#x6027;&#xFF0C;&#x4E0D;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x3002;</p>
<p><strong>&#x6B64;&#x79CD;&#x65B9;&#x5F0F;&#x4F1A;&#x4E3A;&#x7C7B;&#x89C6;&#x56FE;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x90FD;&#x52A0;&#x4E0A;&#x88C5;&#x9970;&#x5668;&#x884C;&#x4E3A;</strong>&#xFF08;&#x56E0;&#x4E3A;&#x662F;&#x5728;&#x89C6;&#x56FE;&#x5165;&#x53E3;&#x5904;&#xFF0C;&#x5206;&#x53D1;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x524D;&#xFF09;&#x3002;</p>
<h3 id="42-&#x5728;&#x7C7B;&#x89C6;&#x56FE;&#x4E2D;&#x88C5;&#x9970;">4.2 &#x5728;&#x7C7B;&#x89C6;&#x56FE;&#x4E2D;&#x88C5;&#x9970;</h3>
<p>&#x5728;&#x7C7B;&#x89C6;&#x56FE;&#x4E2D;&#x4F7F;&#x7528;&#x4E3A;&#x51FD;&#x6570;&#x89C6;&#x56FE;&#x51C6;&#x5907;&#x7684;&#x88C5;&#x9970;&#x5668;&#x65F6;&#xFF0C;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;<strong>method_decorator</strong>&#x5C06;&#x5176;&#x8F6C;&#x6362;&#x4E3A;&#x9002;&#x7528;&#x4E8E;&#x7C7B;&#x89C6;&#x56FE;&#x65B9;&#x6CD5;&#x7684;&#x88C5;&#x9970;&#x5668;&#x3002;</p>
<p><strong>method_decorator&#x88C5;&#x9970;&#x5668;&#x4F7F;&#x7528;name&#x53C2;&#x6570;&#x6307;&#x660E;&#x88AB;&#x88C5;&#x9970;&#x7684;&#x65B9;&#x6CD5;</strong></p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4E3A;&#x5168;&#x90E8;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;</span>
<span class="hljs-decorator">@method_decorator(my_decorator, name=&apos;dispatch&apos;)</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoView</span><span class="hljs-params">(View)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        print(<span class="hljs-string">&apos;get&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        print(<span class="hljs-string">&apos;post&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)


<span class="hljs-comment"># &#x4E3A;&#x7279;&#x5B9A;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;</span>
<span class="hljs-decorator">@method_decorator(my_decorator, name=&apos;get&apos;)</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoView</span><span class="hljs-params">(View)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        print(<span class="hljs-string">&apos;get&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        print(<span class="hljs-string">&apos;post&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)
</code></pre>
<p><strong>&#x5982;&#x679C;&#x9700;&#x8981;&#x4E3A;&#x7C7B;&#x89C6;&#x56FE;&#x7684;&#x591A;&#x4E2A;&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x4F46;&#x53C8;&#x4E0D;&#x662F;&#x6240;&#x6709;&#x7684;&#x65B9;&#x6CD5;&#xFF08;&#x4E3A;&#x6240;&#x6709;&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;&#x53C2;&#x8003;&#x4E0A;&#x9762;&#x4F8B;&#x5B50;&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;&#x7684;&#x65B9;&#x6CD5;&#x4E0A;&#x4F7F;&#x7528;method_decorator&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.utils.decorators <span class="hljs-keyword">import</span> method_decorator

<span class="hljs-comment"># &#x4E3A;&#x7279;&#x5B9A;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoView</span><span class="hljs-params">(View)</span>:</span>

<span class="hljs-decorator">    @method_decorator(my_decorator)  # &#x4E3A;get&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x4E86;&#x88C5;&#x9970;&#x5668;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        print(<span class="hljs-string">&apos;get&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)

<span class="hljs-decorator">    @method_decorator(my_decorator)  # &#x4E3A;post&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x4E86;&#x88C5;&#x9970;&#x5668;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        print(<span class="hljs-string">&apos;post&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">put</span><span class="hljs-params">(self, request)</span>:</span>  <span class="hljs-comment"># &#x6CA1;&#x6709;&#x4E3A;put&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x88C5;&#x9970;&#x5668;</span>
        print(<span class="hljs-string">&apos;put&#x65B9;&#x6CD5;&apos;</span>)
        <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;ok&apos;</span>)
</code></pre>
<h2 id="5-&#x7C7B;&#x89C6;&#x56FE;mixin&#x6269;&#x5C55;&#x7C7B;">5 &#x7C7B;&#x89C6;&#x56FE;Mixin&#x6269;&#x5C55;&#x7C7B;</h2>
<p>&#x4F7F;&#x7528;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x591A;&#x7EE7;&#x627F;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B9A;&#x4E49;&#x7236;&#x7C7B;&#xFF08;&#x4F5C;&#x4E3A;&#x6269;&#x5C55;&#x7C7B;&#xFF09;&#xFF0C;&#x5728;&#x7236;&#x7C7B;&#x4E2D;&#x5B9A;&#x4E49;&#x60F3;&#x8981;&#x5411;&#x7C7B;&#x89C6;&#x56FE;&#x8865;&#x5145;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x7C7B;&#x89C6;&#x56FE;&#x7EE7;&#x627F;&#x8FD9;&#x4E9B;&#x6269;&#x5C55;&#x7236;&#x7C7B;&#xFF0C;&#x4FBF;&#x53EF;&#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x590D;&#x7528;&#x3002;</p>
<p>&#x5B9A;&#x4E49;&#x7684;&#x6269;&#x5C55;&#x7236;&#x7C7B;&#x540D;&#x79F0;&#x901A;&#x5E38;&#x4EE5;Mixin&#x7ED3;&#x5C3E;&#x3002;</p>
<p>&#x4E3E;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ListModelMixin</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    list&#x6269;&#x5C55;&#x7C7B;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">list</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        ...

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CreateModelMixin</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    create&#x6269;&#x5C55;&#x7C7B;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        ...

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BooksView</span><span class="hljs-params">(CreateModelMixin, ListModelMixin, View)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x540C;&#x65F6;&#x7EE7;&#x627F;&#x4E24;&#x4E2A;&#x6269;&#x5C55;&#x7C7B;&#xFF0C;&#x590D;&#x7528;list&#x548C;create&#x65B9;&#x6CD5;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        self.list(request)
        ...

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        self.create(request)
        ...

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SaveOrderView</span><span class="hljs-params">(CreateModelMixin, View)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x7EE7;&#x627F;CreateModelMixin&#x6269;&#x5C55;&#x7C7B;&#xFF0C;&#x590D;&#x7528;create&#x65B9;&#x6CD5;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        self.create(request)
        ...
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C04-ClassViewAndMiddleware/index.html" class="navigation navigation-prev " aria-label="Previous page: 类视图与中间件"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C04-ClassViewAndMiddleware/Middleware.html" class="navigation navigation-next " aria-label="Next page: 中间件"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book django项目 -->
</html>
